package com.rgp.servicepassengeruser.controller;

import com.rgp.internalcommon.common.CommonStatusEnum;
import com.rgp.internalcommon.dto.UserInfoDto;
import com.rgp.internalcommon.dto.ResponseResult;
import com.rgp.servicepassengeruser.pojo.PassengerUser;
import com.rgp.servicepassengeruser.service.PassengerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author nhp
 * @version 1.0
 * @description TODO
 * @date 2024-04-12 22:55
 */
@RestController
public class PassengerUserController {

    @Autowired
    private PassengerUserService passengerUserService;

    @GetMapping("/users/{phone}")
    public ResponseResult<UserInfoDto> getPassengerByPhoneNumber(@PathVariable("phone") String passengerPhone){

        List<PassengerUser> passengerUsers = passengerUserService.getPassengerByPhoneNumber(passengerPhone);
        if (passengerUsers != null && passengerUsers.size() == 1){
            UserInfoDto userInfoDto = new UserInfoDto();
            userInfoDto.setId(passengerUsers.get(0).getId());
            userInfoDto.setUserPhone(passengerUsers.get(0).getPassengerPhone());
            userInfoDto.setUserName(passengerUsers.get(0).getPassengerName());
            userInfoDto.setPassengerGender(passengerUsers.get(0).getPassengerGender());
            userInfoDto.setProfilePhoto(passengerUsers.get(0).getProfilePhoto());
            return ResponseResult.success(userInfoDto);
        }else{
            return ResponseResult.fail(CommonStatusEnum.FAIL_GET_PASSENGER_INFO_ERROR);
        }
    }

    @PostMapping("/users")
    public ResponseResult registPassenger(@RequestBody UserInfoDto userInfoDto){
        Integer result = passengerUserService.registPassenger(userInfoDto);
        if (result == 1){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail(CommonStatusEnum.FAIL_REGIST_PASSENGER_ERROR);
        }
    }
}